window.appModule
    .run(function ($cacheFactory, $rootScope, $log, $state, confirmationPopoverDefaults, localStorageService, ngNotify, $cookies,$window) {
        //对路由错误做统一处理
        $rootScope.$on('$stateChangeError', routeErrorFn);
        $rootScope.$on('$stateNotFound', routeErrorFn);
        ngNotify.config({
            position: 'top',
            duration: 1000
        });
        function routeErrorFn(e) {
            $log.error('router error ');
        }

        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            if (toState.name == 'loginQR' || toState.name == 'loginAccount')return;// 如果是进入登录界面则允许
            // 如果用户不存在
            var user = $cookies.getObject('user');
            console.log('user:' + user);
            if (!user || !user.token) {
                ngNotify.set("您尚未登录, 请先登录", 'error');
                event.preventDefault();// 取消默认跳转行为
                $state.go("loginQR", {from: fromState.name, w: 'notLogin'});//跳转到登录界面
            }
        });
        $rootScope.online = navigator.onLine;
        $window.addEventListener("offline", function() {
            $rootScope.$apply(function() {
                $rootScope.online = false;
            });
        }, false);

        $window.addEventListener("online", function() {
            $rootScope.$apply(function() {
                $rootScope.online = true;
            });
        }, false);
    });
window.appModule.factory('authService', function ($rootScope, $injector, $cookies, $location) {
    return {
        request: function (config) {
            var user = $cookies.getObject('user');
            if (user) {
                var token = user.token;
                console.log('token is ' + token);
                config.headers['token'] = token;
            }
            return config;
        },
        response: function (response) {
            if (response.data.messageCode == "90001") {
                //未登录 跳转到登录页面
                var ngNotify = $injector.get('ngNotify');
                ngNotify.set('您长时间未操作, 需要重新登录', 'error');
                $location.path('/login');
            }
            return response;
        }
    };
});

window.appModule.config(function ($provide, $stateProvider, $urlRouterProvider, $httpProvider) {
    $httpProvider.interceptors.push('authService');
    $urlRouterProvider.when('/', '/login');
    $urlRouterProvider.when('', '/login');

});

window.appModule.directive('resize', function ($window) {
    return function (scope, element, attr) {

        var w = angular.element($window);
        scope.$watch(function () {
            return {
                'height': window.innerHeight,
                'width': window.innerWidth
            };
        }, function (newValue) {
            scope.windowHeight = newValue.height;
            scope.windowWidth = newValue.width;
            scope.resizeWithOffset = function (offsetH) {
                scope.$eval(attr);
                return {
                    'height': (newValue.height - offsetH) + 'px'
                };

            };
            scope.autoScale = function (scaleRate) {
                scope.$eval(attr);
                return {
                    '-weikit-transform': 'scale(' + (newValue.height <= 480 ? 480 : newValue.height) / scaleRate + ')',
                    '-moz-transform': 'scale(' + (newValue.height <= 480 ? 480 : newValue.height) / scaleRate + ')',
                    'transform': 'scale(' + (newValue.height <= 480 ? 480 : newValue.height) / scaleRate + ')'
                };
            };
            scope.resetHeight = function (offset,eId) {
                var eHeight=0;
                if(document.getElementById(eId)){
                    eHeight=document.getElementById(eId).offsetHeight;
                }
                // alert("WH="+newValue.height+",EH="+eHeight);
                scope.$eval(attr);
                if(eHeight<newValue.height){
                    return {
                        'height': (newValue.height - offset) + 'px'
                    };
                }else{
                    return {
                        'height': (eHeight - offset) + 'px'
                    };
                }

            };

        }, true);

        w.bind('resize', function () {
            scope.$apply();
        });
    }
});